package com.dqhl.community.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dqhl.community.R;
import com.dqhl.community.model.RepairRecord;
import com.dqhl.community.util.Config;
import com.dqhl.community.util.ViewHolder;

import org.xutils.x;

import java.util.List;

/**
 * Created by Administrator on 2016/9/3.
 */
public class RepariRecordListAdapter extends BaseAdapter {

    private Context context;
    private List<RepairRecord> repairRecordList;
    private LayoutInflater inflater;
    private onCompleteListener onCompleteListener;
    private onCancleListener onCancleListener;

    public RepariRecordListAdapter(Context contex, List<RepairRecord> repairRecordList,
                                   onCompleteListener onCompleteListener,onCancleListener onCancleListener) {
        this.context = contex;
        this.repairRecordList = repairRecordList;
        this.onCancleListener = onCancleListener;
        this.onCompleteListener = onCompleteListener;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return repairRecordList.size();
    }

    @Override
    public Object getItem(int i) {
        return repairRecordList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = inflater.inflate(R.layout.layout_repairecord, viewGroup, false);
        }
        TextView tv_address = ViewHolder.get(view, R.id.tv_address);
//        TextView tv_building = ViewHolder.get(view,R.id.tv_address_building);
//        TextView tv_unit = ViewHolder.get(view,R.id.tv_address_unit);
//        TextView tv_residence = ViewHolder.get(view,R.id.tv_address_residence);
        TextView tv_name = ViewHolder.get(view, R.id.tv_owner_name);
        TextView tv_telphone = ViewHolder.get(view, R.id.tv_repairItem);
        TextView tv_content_id = ViewHolder.get(view, R.id.tv_repariType);
        TextView tv_addtTime = ViewHolder.get(view, R.id.tv_data);
        TextView tv_content = ViewHolder.get(view, R.id.tv_repairHarry);
        LinearLayout ll_images = ViewHolder.get(view, R.id.ll_images);

        ImageView iv_pic_a = ViewHolder.get(view, R.id.iv_pic_a);
        ImageView iv_pic_b = ViewHolder.get(view, R.id.iv_pic_b);
        ImageView iv_pic_c = ViewHolder.get(view, R.id.iv_pic_c);
        ImageView iv_pic_d = ViewHolder.get(view, R.id.iv_pic_d);
        ImageView iv_cancle = ViewHolder.get(view,R.id.ic_cancle);
        ImageView iv_complete = ViewHolder.get(view,R.id.iv_complete);
        RepairRecord repairRecord = repairRecordList.get(i);

        String strAddress = repairRecord.getCommunity_id() + repairRecord.getBuilding_id() +
                repairRecord.getUnit_id() + repairRecord.getResidence_id();
        tv_address.setText(strAddress);
        tv_name.setText(repairRecord.getName());
        tv_telphone.setText(repairRecord.getTelephone());
        tv_content_id.setText(repairRecord.getContent_id());
        tv_addtTime.setText(repairRecord.getAdd_time());
        //2  已完成  3  已取消
        if("1".equals(repairRecord.getStatus())){
            iv_cancle.setVisibility(View.VISIBLE);
            iv_complete.setVisibility(View.VISIBLE);
        }else if("2".equals(repairRecord.getStatus())){
            iv_complete.setVisibility(View.VISIBLE);
            iv_cancle.setVisibility(View.GONE);
            iv_complete.setImageResource(R.drawable.ic_complete_ready);
        }else if("3".equals(repairRecord.getStatus())){
            iv_cancle.setVisibility(View.VISIBLE);
            iv_complete.setVisibility(View.GONE);
            iv_cancle.setImageResource(R.drawable.ic_cancle_ready);
        }
        if ("".equals(repairRecord.getContent()) && repairRecord.getContent().length() <= 0) {
            tv_content.setText("暂无留言");
        } else {
            tv_content.setText(repairRecord.getContent());
        }
        if ("".equals(Config.Api.img_url + repairRecord.getPic_a()) && "".equals(Config.Api.img_url + repairRecord.getPic_b())
                && "".equals(Config.Api.img_url + repairRecord.getPic_c()) && "".equals(Config.Api.img_url + repairRecord.getPic_d())) {
            ll_images.setVisibility(View.VISIBLE);
        } else {
            x.image().bind(iv_pic_a, Config.Api.img_url + repairRecord.getPic_a());
            x.image().bind(iv_pic_b, Config.Api.img_url + repairRecord.getPic_b());
            x.image().bind(iv_pic_c, Config.Api.img_url + repairRecord.getPic_c());
            x.image().bind(iv_pic_d, Config.Api.img_url + repairRecord.getPic_d());
        }
        iv_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onCancleListener.onCancle(i);
            }
        });
        iv_complete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onCompleteListener.onComplete(i);
            }
        });
        return view;
    }

    public interface onCancleListener{
        void onCancle(int position);
    }
    public interface  onCompleteListener{
        void onComplete(int position);
    }
}
